// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2023 FIXME
// Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree:
//   Copyright (c) 2013, The Linux Foundation. All rights reserved. (FIXME)

#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>

#include <video/mipi_display.h>

#include <drm/drm_mipi_dsi.h>
#include <drm/drm_modes.h>
#include <drm/drm_panel.h>

struct truly_otm1901a {
	struct drm_panel panel;
	struct mipi_dsi_device *dsi;
	struct regulator_bulk_data supplies[2];
	struct gpio_desc *reset_gpio;
	bool prepared;
};

static inline struct truly_otm1901a *to_truly_otm1901a(struct drm_panel *panel)
{
	return container_of(panel, struct truly_otm1901a, panel);
}

static void truly_otm1901a_reset(struct truly_otm1901a *ctx)
{
	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
	usleep_range(5000, 6000);
	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
	usleep_range(5000, 6000);
	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
	usleep_range(5000, 6000);
}

static int truly_otm1901a_on(struct truly_otm1901a *ctx)
{
	struct mipi_dsi_device *dsi = ctx->dsi;
	struct device *dev = &dsi->dev;
	int ret;

	dsi->mode_flags |= MIPI_DSI_MODE_LPM;

	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x19, 0x01, 0x01);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x80);
	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x19, 0x01);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0x1c, 0x33);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0xa0);
	mipi_dsi_dcs_write_seq(dsi, 0xc1, 0xe8);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x82);
	mipi_dsi_dcs_write_seq(dsi, 0xa5, 0x1f);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0xb0);
	mipi_dsi_dcs_write_seq(dsi, 0xca, 0x01);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0xb3);
	mipi_dsi_dcs_write_seq(dsi, 0xca, 0x50);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x90);
	mipi_dsi_dcs_write_seq(dsi, 0xca,
			       0xe6, 0xff, 0x66, 0xff, 0xb3, 0xff, 0x00, 0x00,
			       0x00);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x10);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0xb0, 0xac, 0xda, 0x9c, 0xba, 0xcc, 0x88, 0x88,
			       0x88, 0x78, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
			       0x66, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x11);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0xa0, 0x9d, 0xca, 0x9c, 0xb9, 0xcb, 0x88, 0x88,
			       0x88, 0x78, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
			       0x66, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x12);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0xa0, 0x9c, 0xba, 0x9c, 0xa9, 0xcb, 0x88, 0x88,
			       0x88, 0x78, 0x77, 0x77, 0x77, 0x67, 0x66, 0x66,
			       0x66, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x13);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0xa0, 0x9b, 0xba, 0xab, 0x98, 0xcb, 0x88, 0x88,
			       0x88, 0x78, 0x77, 0x77, 0x77, 0x77, 0x77, 0x66,
			       0x66, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x14);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0xa0, 0x9a, 0xba, 0x9b, 0x89, 0xcb, 0x88, 0x88,
			       0x88, 0x78, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
			       0x66, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x15);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x90, 0x8b, 0xaa, 0x9b, 0x99, 0xca, 0x88, 0x88,
			       0x88, 0x78, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
			       0x77, 0x66);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x16);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x90, 0x9a, 0xa9, 0xaa, 0x98, 0xba, 0x88, 0x88,
			       0x88, 0x88, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x17);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x90, 0x8a, 0x9a, 0x9a, 0x89, 0xba, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x77, 0x77, 0x77, 0x77, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x18);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x90, 0x99, 0x99, 0x9a, 0x89, 0xb9, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x77, 0x77, 0x77, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x19);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x8a, 0x99, 0x9a, 0x98, 0xb8, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x77, 0x77, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1a);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x99, 0x99, 0x99, 0x88, 0xb8, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x77, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1b);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x89, 0x99, 0x99, 0x88, 0xa8, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x77,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1c);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x98, 0x98, 0x99, 0x88, 0xa8, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x78,
			       0x77, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1d);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x88, 0x89, 0x89, 0x89, 0x98, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
			       0x78, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1e);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x88, 0x98, 0x88, 0x89, 0x98, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
			       0x88, 0x77);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x1f);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc7,
			       0x80, 0x88, 0x88, 0x88, 0x88, 0x98, 0x88, 0x88,
			       0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
			       0x88, 0x88);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc6, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b,
			       0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc,
			       0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0xfc, 0x3b, 0xfc, 0x3c, 0xfd, 0x3c, 0xfd, 0x3c,
			       0xfe, 0x3c, 0xfe, 0x3c, 0xfe, 0x3c, 0xff);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x3c, 0xff, 0x3d, 0x00, 0x3d, 0x00, 0x3d, 0x01,
			       0x3d, 0x01, 0x3d, 0x01, 0x3d, 0x02, 0x3d);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x02, 0x3e, 0x02, 0x3e, 0x02, 0x3f, 0x02, 0x3f,
			       0x01, 0x40, 0x01, 0x41, 0x01, 0x41, 0x01);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x42, 0x01, 0x42, 0x01, 0x43, 0x01, 0x44, 0x01,
			       0x44, 0x00, 0x45, 0x00, 0x45, 0x00, 0x46);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x47, 0x00, 0x47, 0x00, 0x47, 0x00, 0x48,
			       0x00, 0x48, 0x00, 0x49, 0x00, 0x49, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x49, 0x00, 0x4a, 0x00, 0x4a, 0x00, 0x4b, 0x00,
			       0x4b, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4c);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x4d, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4b,
			       0x00, 0x4b, 0x00, 0x4b, 0x00, 0x4a, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x4a, 0x00, 0x49, 0x00, 0x49, 0x00, 0x48, 0x00,
			       0x48, 0x00, 0x48, 0x00, 0x47, 0x00, 0x47);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x46, 0x00, 0x46, 0x00, 0x45, 0x00, 0x45,
			       0x00, 0x45, 0x00, 0x44, 0x00, 0x44, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x43, 0x00, 0x43, 0x00, 0x42, 0x00, 0x42, 0x00,
			       0x42, 0x00, 0x41, 0x00, 0x41, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x00, 0x40, 0x00, 0x40, 0xff, 0x3f, 0xff, 0x3f,
			       0xff, 0x3f, 0xff, 0x3e, 0xfe, 0x3e, 0xfe);
	mipi_dsi_dcs_write_seq(dsi, 0xd4,
			       0x3d, 0xfe, 0x3d, 0xfd, 0x3d, 0xfd, 0x3c, 0xfd,
			       0x3c, 0xfd, 0x3c, 0xfc, 0x3b, 0xfc, 0x3b);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e,
			       0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe,
			       0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e,
			       0xff, 0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xff);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xff,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41,
			       0x00, 0x42, 0x00, 0x42, 0x00, 0x43, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x43, 0x00, 0x44, 0x00, 0x44, 0x00, 0x44, 0x00,
			       0x45, 0x00, 0x45, 0x00, 0x46, 0x00, 0x46);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x47, 0x00, 0x47, 0x00, 0x47, 0x00, 0x48,
			       0x00, 0x48, 0x00, 0x49, 0x00, 0x49, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x49, 0x00, 0x4a, 0x00, 0x4a, 0x00, 0x4b, 0x00,
			       0x4b, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4c);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4b,
			       0x00, 0x4b, 0x00, 0x4b, 0x00, 0x4a, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4a, 0x00, 0x49, 0x00, 0x49, 0x00, 0x48, 0x00,
			       0x48, 0x00, 0x48, 0x00, 0x47, 0x00, 0x47);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x46, 0x00, 0x46, 0x00, 0x45, 0x00, 0x45,
			       0x00, 0x45, 0x00, 0x44, 0x00, 0x44, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x43, 0x00, 0x43, 0x00, 0x42, 0x00, 0x42, 0x00,
			       0x42, 0x00, 0x41, 0x00, 0x41, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0xff, 0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xff);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xfe,
			       0x3f, 0xfe, 0x3e, 0xfe, 0x3e, 0xfe, 0x3e);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b,
			       0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc,
			       0x3b, 0xfc, 0x3b, 0xfc, 0x3b, 0xfc, 0x3b);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0xfc, 0x3b, 0xfc, 0x3c, 0xfd, 0x3c, 0xfd, 0x3c,
			       0xfe, 0x3c, 0xfe, 0x3c, 0xfe, 0x3c, 0xff);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3c, 0xff, 0x3d, 0x00, 0x3d, 0x00, 0x3d, 0x01,
			       0x3d, 0x01, 0x3d, 0x01, 0x3d, 0x02, 0x3d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x02, 0x3e, 0x02, 0x3e, 0x02, 0x3f, 0x02, 0x3f,
			       0x01, 0x40, 0x01, 0x41, 0x01, 0x41, 0x01);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x42, 0x01, 0x42, 0x01, 0x43, 0x01, 0x44, 0x01,
			       0x44, 0x00, 0x45, 0x00, 0x45, 0x00, 0x46);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x47, 0x00, 0x47, 0x00, 0x47, 0x00, 0x48,
			       0x00, 0x48, 0x00, 0x49, 0x00, 0x49, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x49, 0x00, 0x4a, 0x00, 0x4a, 0x00, 0x4b, 0x00,
			       0x4b, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4c);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d,
			       0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00,
			       0x4d, 0x00, 0x4d, 0x00, 0x4d, 0x00, 0x4d);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x4d, 0x00, 0x4c, 0x00, 0x4c, 0x00, 0x4b,
			       0x00, 0x4b, 0x00, 0x4b, 0x00, 0x4a, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x4a, 0x00, 0x49, 0x00, 0x49, 0x00, 0x48, 0x00,
			       0x48, 0x00, 0x48, 0x00, 0x47, 0x00, 0x47);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x46, 0x00, 0x46, 0x00, 0x45, 0x00, 0x45,
			       0x00, 0x45, 0x00, 0x44, 0x00, 0x44, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x43, 0x00, 0x43, 0x00, 0x42, 0x00, 0x42, 0x00,
			       0x42, 0x00, 0x41, 0x00, 0x41, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
			       0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,
			       0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x00, 0x40, 0x00, 0x40, 0xff, 0x3f, 0xff, 0x3f,
			       0xff, 0x3f, 0xff, 0x3e, 0xfe, 0x3e, 0xfe);
	mipi_dsi_dcs_write_seq(dsi, 0xd5,
			       0x3d, 0xfe, 0x3d, 0xfd, 0x3d, 0xfd, 0x3c, 0xfd,
			       0x3c, 0xfd, 0x3c, 0xfc, 0x3b, 0xfc, 0x3b);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0xf2);
	mipi_dsi_dcs_write_seq(dsi, 0xc1, 0x00, 0x00, 0x00);
	usleep_range(10000, 11000);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0xda);
	mipi_dsi_dcs_write_seq(dsi, 0xf5, 0x21);
	usleep_range(10000, 11000);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xfb, 0x01);
	usleep_range(10000, 11000);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xeb, 0x10);
	usleep_range(10000, 11000);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x02);
	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x03);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0xc0, 0x00);
	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x02);
	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x01);

	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
	if (ret < 0) {
		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
		return ret;
	}
	msleep(120);

	ret = mipi_dsi_dcs_set_display_brightness(dsi, 0x0000);
	if (ret < 0) {
		dev_err(dev, "Failed to set display brightness: %d\n", ret);
		return ret;
	}

	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);

	ret = mipi_dsi_dcs_set_display_on(dsi);
	if (ret < 0) {
		dev_err(dev, "Failed to set display on: %d\n", ret);
		return ret;
	}
	usleep_range(10000, 11000);

	return 0;
}

static int truly_otm1901a_off(struct truly_otm1901a *ctx)
{
	struct mipi_dsi_device *dsi = ctx->dsi;
	struct device *dev = &dsi->dev;
	int ret;

	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;

	ret = mipi_dsi_dcs_set_display_off(dsi);
	if (ret < 0) {
		dev_err(dev, "Failed to set display off: %d\n", ret);
		return ret;
	}
	msleep(36);

	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
	if (ret < 0) {
		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
		return ret;
	}
	msleep(120);

	return 0;
}

static int truly_otm1901a_prepare(struct drm_panel *panel)
{
	struct truly_otm1901a *ctx = to_truly_otm1901a(panel);
	struct device *dev = &ctx->dsi->dev;
	int ret;

	if (ctx->prepared)
		return 0;

	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
	if (ret < 0) {
		dev_err(dev, "Failed to enable regulators: %d\n", ret);
		return ret;
	}

	truly_otm1901a_reset(ctx);

	ctx->prepared = true;
	return 0;
}

static int truly_otm1901a_enable(struct drm_panel *panel)
{
	struct truly_otm1901a *ctx = to_truly_otm1901a(panel);
	struct device *dev = &ctx->dsi->dev;
	int ret;

	ret = truly_otm1901a_on(ctx);
	if (ret < 0) {
		dev_err(dev, "Failed to initialize panel: %d\n", ret);
		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
		regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
		return ret;
	}

	return 0;
}

static int truly_otm1901a_unprepare(struct drm_panel *panel)
{
	struct truly_otm1901a *ctx = to_truly_otm1901a(panel);

	if (!ctx->prepared)
		return 0;


	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
	regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);

	ctx->prepared = false;
	return 0;
}

static int truly_otm1901a_disable(struct drm_panel *panel)
{
	struct truly_otm1901a *ctx = to_truly_otm1901a(panel);
	struct device *dev = &ctx->dsi->dev;
	int ret;

	ret = truly_otm1901a_off(ctx);
	if (ret < 0)
		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);

	return 0;
}

static const struct drm_display_mode truly_otm1901a_mode = {
	.clock = (720 + 236 + 4 + 32) * (1520 + 10 + 1 + 9) * 60 / 1000,
	.hdisplay = 720,
	.hsync_start = 720 + 236,
	.hsync_end = 720 + 236 + 4,
	.htotal = 720 + 236 + 4 + 32,
	.vdisplay = 1520,
	.vsync_start = 1520 + 10,
	.vsync_end = 1520 + 10 + 1,
	.vtotal = 1520 + 10 + 1 + 9,
	.width_mm = 65,
	.height_mm = 138,
};

static int truly_otm1901a_get_modes(struct drm_panel *panel,
				    struct drm_connector *connector)
{
	struct drm_display_mode *mode;

	mode = drm_mode_duplicate(connector->dev, &truly_otm1901a_mode);
	if (!mode)
		return -ENOMEM;

	drm_mode_set_name(mode);

	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
	connector->display_info.width_mm = mode->width_mm;
	connector->display_info.height_mm = mode->height_mm;
	drm_mode_probed_add(connector, mode);

	return 1;
}

static const struct drm_panel_funcs truly_otm1901a_panel_funcs = {
	.prepare = truly_otm1901a_prepare,
	.enable = truly_otm1901a_enable,
	.unprepare = truly_otm1901a_unprepare,
	.disable= truly_otm1901a_disable,
	.get_modes = truly_otm1901a_get_modes,
};

static int truly_otm1901a_bl_update_status(struct backlight_device *bl)
{
	struct mipi_dsi_device *dsi = bl_get_data(bl);
	u16 brightness = backlight_get_brightness(bl);
	int ret;

	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;

	ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
	if (ret < 0)
		return ret;

	dsi->mode_flags |= MIPI_DSI_MODE_LPM;

	return 0;
}

static const struct backlight_ops truly_otm1901a_bl_ops = {
	.update_status = truly_otm1901a_bl_update_status,
};

static struct backlight_device *
truly_otm1901a_create_backlight(struct mipi_dsi_device *dsi)
{
	struct device *dev = &dsi->dev;
	const struct backlight_properties props = {
		.type = BACKLIGHT_RAW,
		.brightness = 255,
		.max_brightness = 255,
	};

	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
					      &truly_otm1901a_bl_ops, &props);
}

static int truly_otm1901a_probe(struct mipi_dsi_device *dsi)
{
	struct device *dev = &dsi->dev;
	struct truly_otm1901a *ctx;
	int ret;

	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	ctx->supplies[0].supply = "vsn";
	ctx->supplies[1].supply = "vsp";
	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
				      ctx->supplies);
	if (ret < 0)
		return dev_err_probe(dev, ret, "Failed to get regulators\n");

	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(ctx->reset_gpio))
		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
				     "Failed to get reset-gpios\n");

	ctx->dsi = dsi;
	mipi_dsi_set_drvdata(dsi, ctx);

	dsi->lanes = 4;
	dsi->format = MIPI_DSI_FMT_RGB888;
	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
			  MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_NO_EOT_PACKET |
			  MIPI_DSI_CLOCK_NON_CONTINUOUS;

	drm_panel_init(&ctx->panel, dev, &truly_otm1901a_panel_funcs,
		       DRM_MODE_CONNECTOR_DSI);

	ctx->panel.prepare_prev_first = true;

	ctx->panel.backlight = truly_otm1901a_create_backlight(dsi);
	if (IS_ERR(ctx->panel.backlight))
		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
				     "Failed to create backlight\n");

	drm_panel_add(&ctx->panel);

	ret = mipi_dsi_attach(dsi);
	if (ret < 0) {
		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
		drm_panel_remove(&ctx->panel);
		return ret;
	}

	return 0;
}

static void truly_otm1901a_remove(struct mipi_dsi_device *dsi)
{
	struct truly_otm1901a *ctx = mipi_dsi_get_drvdata(dsi);
	int ret;

	ret = mipi_dsi_detach(dsi);
	if (ret < 0)
		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);

	drm_panel_remove(&ctx->panel);
}

static const struct of_device_id truly_otm1901a_of_match[] = {
	{ .compatible = "xiaomi,onclite-otm1901a" }, // FIXME
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, truly_otm1901a_of_match);

static struct mipi_dsi_driver truly_otm1901a_driver = {
	.probe = truly_otm1901a_probe,
	.remove = truly_otm1901a_remove,
	.driver = {
		.name = "panel-truly-otm1901a",
		.of_match_table = truly_otm1901a_of_match,
	},
};
module_mipi_dsi_driver(truly_otm1901a_driver);

MODULE_AUTHOR("linux-mdss-dsi-panel-driver-generator <fix@me>"); // FIXME
MODULE_DESCRIPTION("DRM driver for CSOT otm1901a video mode dsi panel");
MODULE_LICENSE("GPL");
